package com.chen.bpsptsystem.vo;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class LeetCode {

    public static String largestNumber(int[] nums) {
        return Arrays.stream(nums)
                .boxed()
                .map(i -> Integer.toString(i))
                .sorted((s1, s2) -> {
                    String sum1 = s1 + s2;
                    String sum2 = s2 + s1;
                    for (int i = 0; i < sum1.length(); i++) {
                        if (sum1.charAt(i) != sum2.charAt(i)) {
                            return sum2.charAt(i) - sum1.charAt(i);
                        }
                    }
                    return 0;
                })
                .reduce(String::concat)
                .filter(s -> !s.startsWith("0"))
                .orElse("0");
    }

    public static void main(String[] args) {
//        int[] nums = {10, 2, 3, 5, 512};
        int[] nums = {10, 2, 3, 5, 512};
//        //5 512 3 2 10
        System.out.println(largestNumber(nums));
//        System.out.println(largestNumber(nums));

//        System.out.println(ints);
    }

    public static String largeNumber(int[] nums) {
        StringBuffer sb = new StringBuffer();

        Arrays.sort(nums);
        //2 3 5 10 512
        List<Integer> collect = Arrays.stream(nums).boxed().collect(Collectors.toList());
        //转list<string>
        List<String> collect1 = collect.stream().map(i -> Integer.toString(i)).collect(Collectors.toList());
        String x = "";
        for (int i = 0; i < collect1.size(); i++) {
            for (int i1 = i + 1; i1 < collect1.size(); i1++) {
                String temp1 = collect1.get(i) + collect1.get(i1);
                String temp2 = collect1.get(i1) + collect1.get(i);
                if (Integer.parseInt(temp1) - Integer.parseInt(temp2) > 0) {
                    x = temp1;
                }else {
                    x = temp2;
                }
            }
            x = x + x;
        }

        return x;
    }
}
